package net.hn.hnms.biz.job.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import net.hn.hnms.biz.job.domain.CarInspect;
import net.hn.hnms.biz.job.repository.CarInspectRepository;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Author: PanSX
 * Date: 2023/3/24 8:34
 */
@Slf4j
@Service
public class InspectService {
    @Autowired
    private CarInspectRepository carInspectRepository;
    @Autowired
    private RestTemplate restTemplate;

    @XxlJob("carInspectHandler")
    public void carInspect() {
//        String url = "http://10.33.162.131:8085/vds/record/getFinishedList";
        String url = "http://192.168.4.200:8085/vds/record/getFinishedList";
        Map body = new HashMap();
        body.put("pageIndex", 1);
        body.put("pageSize", 90000);
        String jobParam = XxlJobHelper.getJobParam();
        if(StringUtils.isNotBlank(jobParam)){
            body.put("updateTime", jobParam);
        }else{
            Calendar instance = Calendar.getInstance();
            instance.set(Calendar.DAY_OF_MONTH,instance.get(Calendar.DAY_OF_MONTH)-1);
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            body.put("updateTime", sdf.format(instance.getTime()));
        }

        String jsonData = JSON.toJSONString(body);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity<>(jsonData, headers);
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(url, request, JSONObject.class);
        JSONObject json = responseEntity.getBody();
//        log.info(json.toJSONString());
        if (json.getInteger("code") == 100) {
            JSONObject data = json.getJSONObject("data");
            if (data != null) {
                JSONArray list = data.getJSONArray("list");
                List<CarInspect> carInspects = list.toJavaList(CarInspect.class);
                for (CarInspect inspect:carInspects){
                    inspect.setDetail(carInspectDetail(inspect.getId()));
                }
                carInspectRepository.saveAll(carInspects);
                log.info("检车更新数据："+carInspects.size());
            }
        }
    }

    public String carInspectDetail(Integer id) {
//        String url="http://10.33.162.131:8085/vds/record/getDetail";
        String url="http://192.168.4.200:8085/vds/record/getDetail";
        Map body = new HashMap();
        body.put("id", id);
        String jsonData = JSON.toJSONString(body);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity<>(jsonData, headers);
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(url, request, JSONObject.class);
        JSONObject json = responseEntity.getBody();
//        log.info(json.toJSONString());
        if (json.getInteger("code") == 100) {
            return json.getJSONObject("data").toJSONString();
        }
        return null;
    }
}
